import 'package:animal/network/api.dart';
import 'package:animal/socket/socket_client.dart';
import 'package:animal/ui/home/page/home_index_page.dart';
import 'package:animal/ui/login/NWApi.dart';
import 'package:animal/ui/login/controller/short_login_controller.dart';
import 'package:animal/ui/login/model/short_login_model.dart';
import 'package:animal/ui/login/page/forget_pwd_page.dart';
import 'package:animal/utils/RuleUtils.dart';
import 'package:animal/utils/index.dart';
import 'package:animal/utils/sputils/sp_config.dart';
import 'package:flutter/material.dart';
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
import 'package:get/get.dart';

class PwdLoginController extends GetxController {
  //手机号内容
  final TextEditingController phoneController = TextEditingController();

  //密码内容
  final TextEditingController pwdController = TextEditingController();

  //勾选按钮，默认true
  RxBool checkBtn = true.obs;

  //是否密文显示
  RxBool obscure = true.obs;

  //是否显示删除按钮
  RxBool showDeleteBtn = false.obs;

  var user = User().obs;

  final ShortLoginController shortLogin = Get.find();
  final ClientSocket clientSocket = Get.find();

  @override
  void onInit() {
    super.onInit();
    phoneController.addListener(() {
      if (phoneController.text.isNotEmpty) {
        showDeleteBtn.value = true;
      } else {
        showDeleteBtn.value = false;
      }
    });
  }

  @override
  void onReady() {
    super.onReady();
    shortLogin.checkBtn.value = checkBtn.value;
  }

  ///登录
  void login() {
    if (!phoneController.text.isNotEmpty) {
      SmartDialog.showToast("请输入手机号码");
      return;
    }
    if (RuleUtils().checkStringRule(RuleType.phone, phoneController.text, null) != null) {
      SmartDialog.showToast("手机号码错误，请重新输入");
      return;
    }
    if (!pwdController.text.isNotEmpty) {
      SmartDialog.showToast("请输入密码");
      return;
    }
    if (pwdController.text.length < 6) {
      SmartDialog.showToast("密码长度不能小于6位");
      return;
    }
    if (!checkBtn.value) {
      SmartDialog.showToast("请勾选《用户协议》和《隐私政策》");
      return;
    }
    var putData = {"mobile": phoneController.text, "pwd": pwdController.text};
    SmartDialog.showLoading();
    HttpManager().post<ShortLoginModel>(NWApi.userLoginPwdLogin, queryParameters: putData).listen((value) {
      SmartDialog.dismiss();
      user.value = value.data!.user!;
      SpConfig().setUserToken(user.value.token!);
      SpConfig().setUserId(user.value.id!);
      SpConfig().setUserInfo(jsonEncode(user.value));
      clientSocket.connect(Get.context);
      Get.off(const HomeIndexPage());
    }, onError: (err) {
      SmartDialog.dismiss();
      SmartDialog.showToast(err.message);
      debugPrint(err.message);
    });
  }

  ///勾选按钮事件
  void checkTap() {
    checkBtn.value = !checkBtn.value;
  }

  ///前往用户协议
  void gotoUserAgreement() {
    SmartDialog.showToast("前往用户协议", alignment: Alignment.center);
  }

  ///前往隐私协议
  void gotoPrivacy() {
    SmartDialog.showToast("前往隐私协议", alignment: Alignment.center);
  }

  ///前往忘记密码页面
  void gotoForgetPwd() {
    Get.to(const ForGetPwdPage());
  }

  ///清楚手机号码
  void clearPhoneNumber() {
    phoneController.clear();
  }
}
